package org.jeecg.modules.rental.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.rental.entity.RegionalProfits;
import org.jeecg.modules.rental.service.IRegionalProfitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 区域收入表
 * @Author: jeecg-boot
 * @Date:   2025-02-26
 * @Version: V1.0
 */
@Api(tags="区域收入表")
@RestController
@RequestMapping("/rental/regionalProfits")
@Slf4j
public class RegionalProfitsController extends JeecgController<RegionalProfits, IRegionalProfitsService> {
	@Autowired
	private IRegionalProfitsService regionalProfitsService;
	
	/**
	 * 分页列表查询
	 *
	 * @param regionalProfits
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "区域收入表-分页列表查询")
	@ApiOperation(value="区域收入表-分页列表查询", notes="区域收入表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RegionalProfits>> queryPageList(RegionalProfits regionalProfits,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RegionalProfits> queryWrapper = QueryGenerator.initQueryWrapper(regionalProfits, req.getParameterMap());
		Page<RegionalProfits> page = new Page<RegionalProfits>(pageNo, pageSize);
		IPage<RegionalProfits> pageList = regionalProfitsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param regionalProfits
	 * @return
	 */
	@AutoLog(value = "区域收入表-添加")
	@ApiOperation(value="区域收入表-添加", notes="区域收入表-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:regional_profits:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RegionalProfits regionalProfits) {
		regionalProfitsService.save(regionalProfits);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param regionalProfits
	 * @return
	 */
	@AutoLog(value = "区域收入表-编辑")
	@ApiOperation(value="区域收入表-编辑", notes="区域收入表-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:regional_profits:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RegionalProfits regionalProfits) {
		regionalProfitsService.updateById(regionalProfits);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "区域收入表-通过id删除")
	@ApiOperation(value="区域收入表-通过id删除", notes="区域收入表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:regional_profits:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		regionalProfitsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "区域收入表-批量删除")
	@ApiOperation(value="区域收入表-批量删除", notes="区域收入表-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:regional_profits:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.regionalProfitsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "区域收入表-通过id查询")
	@ApiOperation(value="区域收入表-通过id查询", notes="区域收入表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RegionalProfits> queryById(@RequestParam(name="id",required=true) String id) {
		RegionalProfits regionalProfits = regionalProfitsService.getById(id);
		if(regionalProfits==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(regionalProfits);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param regionalProfits
    */
    //@RequiresPermissions("org.jeecg.modules.demo:regional_profits:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RegionalProfits regionalProfits) {
        return super.exportXls(request, regionalProfits, RegionalProfits.class, "区域收入表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("regional_profits:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RegionalProfits.class);
    }

	 @ApiOperation("区域收益统计")
	 @GetMapping("/statistics")
	 public Result<List<Map<String, Object>>> getRegionalIncomeStatistics(
			 @RequestParam(value = "type", required = true) String type
	 ) {
		 try {
			 // 调用服务层统计区域收益
			 List<Map<String, Object>> result = regionalProfitsService.getRegionalIncomeStatistics(type);
			 return Result.OK(result);
		 } catch (Exception e) {
			 log.error("区域收益统计失败", e);
			 return Result.error("查询失败：" + e.getMessage());
		 }
	 }

}
